<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mockclock_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_mockclock_test.js.html">mockclock_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.MockClockTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.MockClockTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.events&#39;);
<a name="line19"></a>goog.require(&#39;goog.functions&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line24"></a>
<a name="line25"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line26"></a>
<a name="line27"></a>function tearDown() {
<a name="line28"></a>  stubs.reset();
<a name="line29"></a>}
<a name="line30"></a>
<a name="line31"></a>function testMockClockWasInstalled() {
<a name="line32"></a>  var clock = new goog.testing.MockClock();
<a name="line33"></a>  var originalTimeout = window.setTimeout;
<a name="line34"></a>  clock.install();
<a name="line35"></a>  assertNotEquals(window.setTimeout, originalTimeout);
<a name="line36"></a>  setTimeout(function() {}, 100);
<a name="line37"></a>  assertEquals(1, clock.getTimeoutsMade());
<a name="line38"></a>  setInterval(function() {}, 200);
<a name="line39"></a>  assertEquals(2, clock.getTimeoutsMade());
<a name="line40"></a>  clock.uninstall();
<a name="line41"></a>  assertEquals(window.setTimeout, originalTimeout);
<a name="line42"></a>  assertNull(clock.replacer_);
<a name="line43"></a>}
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>function testSetTimeoutAndTick() {
<a name="line47"></a>  var clock = new goog.testing.MockClock(true);
<a name="line48"></a>  var m5 = false, m10 = false, m15 = false, m20 = false;
<a name="line49"></a>  setTimeout(function() { m5 = true; }, 5);
<a name="line50"></a>  setTimeout(function() { m10 = true; }, 10);
<a name="line51"></a>  setTimeout(function() { m15 = true; }, 15);
<a name="line52"></a>  setTimeout(function() { m20 = true; }, 20);
<a name="line53"></a>  assertEquals(4, clock.getTimeoutsMade());
<a name="line54"></a>
<a name="line55"></a>  assertEquals(4, clock.tick(4));
<a name="line56"></a>  assertEquals(4, clock.getCurrentTime());
<a name="line57"></a>
<a name="line58"></a>  assertFalse(m5);
<a name="line59"></a>  assertFalse(m10);
<a name="line60"></a>  assertFalse(m15);
<a name="line61"></a>  assertFalse(m20);
<a name="line62"></a>
<a name="line63"></a>  assertEquals(5, clock.tick(1));
<a name="line64"></a>  assertEquals(5, clock.getCurrentTime());
<a name="line65"></a>
<a name="line66"></a>  assertTrue(&#39;m5 should now be true&#39;, m5);
<a name="line67"></a>  assertFalse(m10);
<a name="line68"></a>  assertFalse(m15);
<a name="line69"></a>  assertFalse(m20);
<a name="line70"></a>
<a name="line71"></a>  assertEquals(10, clock.tick(5));
<a name="line72"></a>  assertEquals(10, clock.getCurrentTime());
<a name="line73"></a>
<a name="line74"></a>  assertTrue(&#39;m5 should be true&#39;, m5);
<a name="line75"></a>  assertTrue(&#39;m10 should now be true&#39;, m10);
<a name="line76"></a>  assertFalse(m15);
<a name="line77"></a>  assertFalse(m20);
<a name="line78"></a>
<a name="line79"></a>  assertEquals(15, clock.tick(5));
<a name="line80"></a>  assertEquals(15, clock.getCurrentTime());
<a name="line81"></a>
<a name="line82"></a>  assertTrue(&#39;m5 should be true&#39;, m5);
<a name="line83"></a>  assertTrue(&#39;m10 should be true&#39;, m10);
<a name="line84"></a>  assertTrue(&#39;m15 should now be true&#39;, m15);
<a name="line85"></a>  assertFalse(m20);
<a name="line86"></a>
<a name="line87"></a>  assertEquals(20, clock.tick(5));
<a name="line88"></a>  assertEquals(20, clock.getCurrentTime());
<a name="line89"></a>
<a name="line90"></a>  assertTrue(&#39;m5 should be true&#39;, m5);
<a name="line91"></a>  assertTrue(&#39;m10 should be true&#39;, m10);
<a name="line92"></a>  assertTrue(&#39;m15 should be true&#39;, m15);
<a name="line93"></a>  assertTrue(&#39;m20 should now be true&#39;, m20);
<a name="line94"></a>
<a name="line95"></a>  clock.uninstall();
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>function testSetImmediateAndTick() {
<a name="line100"></a>  var clock = new goog.testing.MockClock(true);
<a name="line101"></a>  var tick0 = false;
<a name="line102"></a>  var tick1 = false;
<a name="line103"></a>  setImmediate(function() { tick0 = true; });
<a name="line104"></a>  setImmediate(function() { tick1 = true; });
<a name="line105"></a>  assertEquals(2, clock.getTimeoutsMade());
<a name="line106"></a>
<a name="line107"></a>  clock.tick(0);
<a name="line108"></a>  assertTrue(tick0);
<a name="line109"></a>  assertTrue(tick1);
<a name="line110"></a>
<a name="line111"></a>  clock.uninstall();
<a name="line112"></a>}
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>function testSetInterval() {
<a name="line116"></a>  var clock = new goog.testing.MockClock(true);
<a name="line117"></a>  var times = 0;
<a name="line118"></a>  setInterval(function() { times++; }, 100);
<a name="line119"></a>
<a name="line120"></a>  clock.tick(500);
<a name="line121"></a>  assertEquals(5, times);
<a name="line122"></a>  clock.tick(100);
<a name="line123"></a>  assertEquals(6, times);
<a name="line124"></a>  clock.tick(100);
<a name="line125"></a>  assertEquals(7, times);
<a name="line126"></a>  clock.tick(50);
<a name="line127"></a>  assertEquals(7, times);
<a name="line128"></a>  clock.tick(50);
<a name="line129"></a>  assertEquals(8, times);
<a name="line130"></a>
<a name="line131"></a>  clock.uninstall();
<a name="line132"></a>}
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>function testRequestAnimationFrame() {
<a name="line136"></a>  goog.global.requestAnimationFrame = function() {
<a name="line137"></a>  };
<a name="line138"></a>  var clock = new goog.testing.MockClock(true);
<a name="line139"></a>  var times = [];
<a name="line140"></a>  var recFunc = goog.testing.recordFunction(function(now) {
<a name="line141"></a>    times.push(now);
<a name="line142"></a>  });
<a name="line143"></a>  goog.global.requestAnimationFrame(recFunc);
<a name="line144"></a>  clock.tick(50);
<a name="line145"></a>  assertEquals(1, recFunc.getCallCount());
<a name="line146"></a>  assertEquals(20, times[0]);
<a name="line147"></a>
<a name="line148"></a>  goog.global.requestAnimationFrame(recFunc);
<a name="line149"></a>  clock.tick(100);
<a name="line150"></a>  assertEquals(2, recFunc.getCallCount());
<a name="line151"></a>  assertEquals(70, times[1]);
<a name="line152"></a>
<a name="line153"></a>  clock.uninstall();
<a name="line154"></a>}
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>function testClearTimeout() {
<a name="line158"></a>  var clock = new goog.testing.MockClock(true);
<a name="line159"></a>  var ran = false;
<a name="line160"></a>  var c = setTimeout(function() { ran = true; }, 100);
<a name="line161"></a>  clock.tick(50);
<a name="line162"></a>  assertFalse(ran);
<a name="line163"></a>  clearTimeout(c);
<a name="line164"></a>  clock.tick(100);
<a name="line165"></a>  assertFalse(ran);
<a name="line166"></a>  clock.uninstall();
<a name="line167"></a>}
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>function testClearInterval() {
<a name="line171"></a>  var clock = new goog.testing.MockClock(true);
<a name="line172"></a>  var times = 0;
<a name="line173"></a>  var c = setInterval(function() { times++; }, 100);
<a name="line174"></a>
<a name="line175"></a>  clock.tick(500);
<a name="line176"></a>  assertEquals(5, times);
<a name="line177"></a>  clock.tick(100);
<a name="line178"></a>  assertEquals(6, times);
<a name="line179"></a>  clock.tick(100);
<a name="line180"></a>  clearInterval(c);
<a name="line181"></a>  assertEquals(7, times);
<a name="line182"></a>  clock.tick(50);
<a name="line183"></a>  assertEquals(7, times);
<a name="line184"></a>  clock.tick(50);
<a name="line185"></a>  assertEquals(7, times);
<a name="line186"></a>
<a name="line187"></a>  clock.uninstall();
<a name="line188"></a>}
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>function testClearInterval2() {
<a name="line192"></a>  // Tests that we can clear the interval from inside the function
<a name="line193"></a>  var clock = new goog.testing.MockClock(true);
<a name="line194"></a>  var times = 0;
<a name="line195"></a>  var c = setInterval(function() {
<a name="line196"></a>    times++;
<a name="line197"></a>    if (times == 6) {
<a name="line198"></a>      clearInterval(c);
<a name="line199"></a>    }
<a name="line200"></a>  }, 100);
<a name="line201"></a>
<a name="line202"></a>  clock.tick(500);
<a name="line203"></a>  assertEquals(5, times);
<a name="line204"></a>  clock.tick(100);
<a name="line205"></a>  assertEquals(6, times);
<a name="line206"></a>  clock.tick(100);
<a name="line207"></a>  assertEquals(6, times);
<a name="line208"></a>  clock.tick(50);
<a name="line209"></a>  assertEquals(6, times);
<a name="line210"></a>  clock.tick(50);
<a name="line211"></a>  assertEquals(6, times);
<a name="line212"></a>
<a name="line213"></a>  clock.uninstall();
<a name="line214"></a>}
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>function testCancelRequestAnimationFrame() {
<a name="line218"></a>  goog.global.requestAnimationFrame = function() {
<a name="line219"></a>  };
<a name="line220"></a>  goog.global.cancelRequestAnimationFrame = function() {
<a name="line221"></a>  };
<a name="line222"></a>  var clock = new goog.testing.MockClock(true);
<a name="line223"></a>  var ran = false;
<a name="line224"></a>  var c = goog.global.requestAnimationFrame(function() { ran = true; });
<a name="line225"></a>  clock.tick(10);
<a name="line226"></a>  assertFalse(ran);
<a name="line227"></a>  goog.global.cancelRequestAnimationFrame(c);
<a name="line228"></a>  clock.tick(20);
<a name="line229"></a>  assertFalse(ran);
<a name="line230"></a>  clock.uninstall();
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>function testMockGoogNow() {
<a name="line235"></a>  assertNotEquals(0, goog.now());
<a name="line236"></a>  var clock = new goog.testing.MockClock(true);
<a name="line237"></a>  assertEquals(0, goog.now());
<a name="line238"></a>  clock.tick(50);
<a name="line239"></a>  assertEquals(50, goog.now());
<a name="line240"></a>  clock.uninstall();
<a name="line241"></a>  assertNotEquals(50, goog.now());
<a name="line242"></a>}
<a name="line243"></a>
<a name="line244"></a>
<a name="line245"></a>function testTimeoutDelay() {
<a name="line246"></a>  var clock = new goog.testing.MockClock(true);
<a name="line247"></a>  var m5 = false, m10 = false, m20 = false;
<a name="line248"></a>  setTimeout(function() { m5 = true; }, 5);
<a name="line249"></a>  setTimeout(function() { m10 = true; }, 10);
<a name="line250"></a>  setTimeout(function() { m20 = true; }, 20);
<a name="line251"></a>
<a name="line252"></a>  // Fire 3ms early, so m5 fires at t=2
<a name="line253"></a>  clock.setTimeoutDelay(-3);
<a name="line254"></a>  clock.tick(1);
<a name="line255"></a>  assertFalse(m5);
<a name="line256"></a>  assertFalse(m10);
<a name="line257"></a>  clock.tick(1);
<a name="line258"></a>  assertTrue(m5);
<a name="line259"></a>  assertFalse(m10);
<a name="line260"></a>
<a name="line261"></a>  // Fire 3ms late, so m10 fires at t=13
<a name="line262"></a>  clock.setTimeoutDelay(3);
<a name="line263"></a>  assertEquals(12, clock.tick(10));
<a name="line264"></a>  assertEquals(12, clock.getCurrentTime());
<a name="line265"></a>  assertFalse(m10);
<a name="line266"></a>  clock.tick(1);
<a name="line267"></a>  assertTrue(m10);
<a name="line268"></a>  assertFalse(m20);
<a name="line269"></a>
<a name="line270"></a>  // Fire 10ms early, so m20 fires now, since it&#39;s after t=10
<a name="line271"></a>  clock.setTimeoutDelay(-10);
<a name="line272"></a>  assertFalse(m20);
<a name="line273"></a>  assertEquals(14, clock.tick(1));
<a name="line274"></a>  assertEquals(14, clock.getCurrentTime());
<a name="line275"></a>  assertTrue(m20);
<a name="line276"></a>
<a name="line277"></a>  clock.uninstall();
<a name="line278"></a>}
<a name="line279"></a>
<a name="line280"></a>
<a name="line281"></a>function testTimerCallbackCanCreateIntermediateTimer() {
<a name="line282"></a>  var clock = new goog.testing.MockClock(true);
<a name="line283"></a>  var sequence = [];
<a name="line284"></a>
<a name="line285"></a>  // Create 3 timers: 1, 2, and 3.  Timer 1 should fire at T=1, timer 2 at
<a name="line286"></a>  // T=2, and timer 3 at T=3.  The catch: Timer 2 is created by the
<a name="line287"></a>  // callback within timer 0.
<a name="line288"></a>
<a name="line289"></a>  // Testing method: Create a simple string sequencing each timer and at
<a name="line290"></a>  // what time it fired.
<a name="line291"></a>
<a name="line292"></a>  setTimeout(function() {
<a name="line293"></a>    sequence.push(&#39;timer1 at T=&#39; + goog.now());
<a name="line294"></a>    setTimeout(function() {
<a name="line295"></a>      sequence.push(&#39;timer2 at T=&#39; + goog.now());
<a name="line296"></a>    }, 1);
<a name="line297"></a>  }, 1);
<a name="line298"></a>
<a name="line299"></a>  setTimeout(function() {
<a name="line300"></a>    sequence.push(&#39;timer3 at T=&#39; + goog.now());
<a name="line301"></a>  }, 3);
<a name="line302"></a>
<a name="line303"></a>  clock.tick(4);
<a name="line304"></a>
<a name="line305"></a>  assertEquals(
<a name="line306"></a>      &#39;Each timer should fire in sequence at the correct time.&#39;,
<a name="line307"></a>      &#39;timer1 at T=1, timer2 at T=2, timer3 at T=3&#39;,
<a name="line308"></a>      sequence.join(&#39;, &#39;));
<a name="line309"></a>
<a name="line310"></a>  clock.uninstall();
<a name="line311"></a>}
<a name="line312"></a>
<a name="line313"></a>
<a name="line314"></a>function testCorrectArgumentsPassedToCallback() {
<a name="line315"></a>  var clock = new goog.testing.MockClock(true);
<a name="line316"></a>  var timeoutId;
<a name="line317"></a>  var timeoutExecuted = false;
<a name="line318"></a>
<a name="line319"></a>  timeoutId = setTimeout(function(arg) {
<a name="line320"></a>    assertEquals(&#39;&quot;this&quot; must be goog.global&#39;,
<a name="line321"></a>        goog.global, this);
<a name="line322"></a>    assertEquals(&#39;The timeout ID must be the first parameter&#39;,
<a name="line323"></a>        timeoutId, arg);
<a name="line324"></a>    assertEquals(&#39;Exactly one argument must be passed&#39;,
<a name="line325"></a>        1, arguments.length);
<a name="line326"></a>    timeoutExecuted = true;
<a name="line327"></a>  }, 1);
<a name="line328"></a>
<a name="line329"></a>  clock.tick(4);
<a name="line330"></a>
<a name="line331"></a>  assertTrue(&#39;The timeout was not executed&#39;, timeoutExecuted);
<a name="line332"></a>
<a name="line333"></a>  clock.uninstall();
<a name="line334"></a>}
<a name="line335"></a>
<a name="line336"></a>
<a name="line337"></a>function testTickZero() {
<a name="line338"></a>  var clock = new goog.testing.MockClock(true);
<a name="line339"></a>  var calls = 0;
<a name="line340"></a>
<a name="line341"></a>  setTimeout(function() {
<a name="line342"></a>    assertEquals(&#39;I need to be first&#39;, 0, calls);
<a name="line343"></a>    calls++;
<a name="line344"></a>  }, 0);
<a name="line345"></a>
<a name="line346"></a>  setTimeout(function() {
<a name="line347"></a>    assertEquals(&#39;I need to be second&#39;, 1, calls);
<a name="line348"></a>    calls++;
<a name="line349"></a>  }, 0);
<a name="line350"></a>
<a name="line351"></a>  clock.tick(0);
<a name="line352"></a>  assertEquals(2, calls);
<a name="line353"></a>
<a name="line354"></a>  setTimeout(function() {
<a name="line355"></a>    assertEquals(&#39;I need to be third&#39;, 2, calls);
<a name="line356"></a>    calls++;
<a name="line357"></a>  }, 0);
<a name="line358"></a>
<a name="line359"></a>  clock.tick(0);
<a name="line360"></a>  assertEquals(3, calls);
<a name="line361"></a>
<a name="line362"></a>  assertEquals(&#39;Time should still be zero&#39;, 0, goog.now());
<a name="line363"></a>
<a name="line364"></a>  clock.uninstall();
<a name="line365"></a>}
<a name="line366"></a>
<a name="line367"></a>
<a name="line368"></a>function testReset() {
<a name="line369"></a>  var clock = new goog.testing.MockClock(true);
<a name="line370"></a>
<a name="line371"></a>  setTimeout(function() {
<a name="line372"></a>    fail(&#39;Timeouts should be cleared after a reset&#39;);
<a name="line373"></a>  }, 0);
<a name="line374"></a>
<a name="line375"></a>  clock.reset();
<a name="line376"></a>  clock.tick(999999);
<a name="line377"></a>  clock.uninstall();
<a name="line378"></a>}
<a name="line379"></a>
<a name="line380"></a>
<a name="line381"></a>function testQueueInsertionHelper() {
<a name="line382"></a>  var queue = [];
<a name="line383"></a>
<a name="line384"></a>  function queueToString() {
<a name="line385"></a>    var buffer = [];
<a name="line386"></a>    for (var i = 0; i &lt; queue.length; i++) {
<a name="line387"></a>      buffer.push(queue[i].runAtMillis);
<a name="line388"></a>    }
<a name="line389"></a>    return buffer.join(&#39;,&#39;);
<a name="line390"></a>  }
<a name="line391"></a>
<a name="line392"></a>  goog.testing.MockClock.insert_({runAtMillis: 2}, queue);
<a name="line393"></a>  assertEquals(&#39;Only item&#39;,
<a name="line394"></a>      &#39;2&#39;, queueToString());
<a name="line395"></a>
<a name="line396"></a>  goog.testing.MockClock.insert_({runAtMillis: 4}, queue);
<a name="line397"></a>  assertEquals(&#39;Biggest item&#39;,
<a name="line398"></a>      &#39;4,2&#39;, queueToString());
<a name="line399"></a>
<a name="line400"></a>  goog.testing.MockClock.insert_({runAtMillis: 5}, queue);
<a name="line401"></a>  assertEquals(&#39;An even bigger item&#39;,
<a name="line402"></a>      &#39;5,4,2&#39;, queueToString());
<a name="line403"></a>
<a name="line404"></a>  goog.testing.MockClock.insert_({runAtMillis: 1}, queue);
<a name="line405"></a>  assertEquals(&#39;Smallest item&#39;,
<a name="line406"></a>      &#39;5,4,2,1&#39;, queueToString());
<a name="line407"></a>
<a name="line408"></a>  goog.testing.MockClock.insert_({runAtMillis: 1, dup: true}, queue);
<a name="line409"></a>  assertEquals(&#39;Duplicate smallest item&#39;,
<a name="line410"></a>      &#39;5,4,2,1,1&#39;, queueToString());
<a name="line411"></a>  assertTrue(&#39;Duplicate item comes at a smaller index&#39;, queue[3].dup);
<a name="line412"></a>
<a name="line413"></a>  goog.testing.MockClock.insert_({runAtMillis: 3}, queue);
<a name="line414"></a>  goog.testing.MockClock.insert_({runAtMillis: 3, dup: true}, queue);
<a name="line415"></a>  assertEquals(&#39;Duplicate a middle item&#39;,
<a name="line416"></a>      &#39;5,4,3,3,2,1,1&#39;, queueToString());
<a name="line417"></a>  assertTrue(&#39;Duplicate item comes at a smaller index&#39;, queue[2].dup);
<a name="line418"></a>}
<a name="line419"></a>
<a name="line420"></a>function testIsTimeoutSet() {
<a name="line421"></a>  var clock = new goog.testing.MockClock(true);
<a name="line422"></a>  var timeoutKey = setTimeout(function() {}, 1);
<a name="line423"></a>  assertTrue(&#39;Timeout &#39; + timeoutKey + &#39; should be set&#39;,
<a name="line424"></a>      clock.isTimeoutSet(timeoutKey));
<a name="line425"></a>  var nextTimeoutKey = timeoutKey + 1;
<a name="line426"></a>  assertFalse(&#39;Timeout &#39; + nextTimeoutKey + &#39; should not be set&#39;,
<a name="line427"></a>      clock.isTimeoutSet(nextTimeoutKey));
<a name="line428"></a>  clearTimeout(timeoutKey);
<a name="line429"></a>  assertFalse(&#39;Timeout &#39; + timeoutKey + &#39; should no longer be set&#39;,
<a name="line430"></a>      clock.isTimeoutSet(timeoutKey));
<a name="line431"></a>  var newTimeoutKey = setTimeout(function() {}, 1);
<a name="line432"></a>  clock.tick(5);
<a name="line433"></a>  assertFalse(&#39;Timeout &#39; + timeoutKey + &#39; should not be set&#39;,
<a name="line434"></a>      clock.isTimeoutSet(timeoutKey));
<a name="line435"></a>  assertTrue(&#39;Timeout &#39; + newTimeoutKey + &#39; should be set&#39;,
<a name="line436"></a>      clock.isTimeoutSet(newTimeoutKey));
<a name="line437"></a>  clock.uninstall();
<a name="line438"></a>}
<a name="line439"></a>
<a name="line440"></a>function testBalksOnTimeoutsGreaterThanMaxInt() {
<a name="line441"></a>  // Browsers have trouble with timeout greater than max int, so we
<a name="line442"></a>  // want Mock Clock to fail if this happens.
<a name="line443"></a>  var clock = new goog.testing.MockClock(true);
<a name="line444"></a>  // Functions on window don&#39;t seem to be able to throw exceptions in
<a name="line445"></a>  // IE6.  Explicitly reading the property makes it work.
<a name="line446"></a>  var setTimeout = window.setTimeout;
<a name="line447"></a>  assertThrows(&#39;Timeouts &gt; MAX_INT should fail&#39;,
<a name="line448"></a>      function() {
<a name="line449"></a>        setTimeout(goog.nullFunction, 2147483648);
<a name="line450"></a>      });
<a name="line451"></a>  assertThrows(&#39;Timeouts much greater than MAX_INT should fail&#39;,
<a name="line452"></a>      function() {
<a name="line453"></a>        setTimeout(goog.nullFunction, 2147483648 * 10);
<a name="line454"></a>      });
<a name="line455"></a>  clock.uninstall();
<a name="line456"></a>}
<a name="line457"></a>
<a name="line458"></a>function testCorrectSetTimeoutIsRestored() {
<a name="line459"></a>  var safe = goog.functions.error(&#39;should not have been called&#39;);
<a name="line460"></a>  stubs.set(window, &#39;setTimeout&#39;, safe);
<a name="line461"></a>
<a name="line462"></a>  var clock = new goog.testing.MockClock(true);
<a name="line463"></a>  assertNotEquals(&#39;setTimeout is replaced&#39;, safe, window.setTimeout);
<a name="line464"></a>  clock.uninstall();
<a name="line465"></a>  // NOTE: If this assertion proves to be flaky in IE, the string value of
<a name="line466"></a>  // the two functions have to be compared as described in
<a name="line467"></a>  // goog.testing.TestCase#finalize.
<a name="line468"></a>  assertEquals(&#39;setTimeout is restored&#39;, safe, window.setTimeout);
<a name="line469"></a>}
<a name="line470"></a>
<a name="line471"></a>function testMozRequestAnimationFrame() {
<a name="line472"></a>  // Setting this function will indirectly tell the mock clock to mock it out.
<a name="line473"></a>  stubs.set(window, &#39;mozRequestAnimationFrame&#39;, goog.nullFunction);
<a name="line474"></a>
<a name="line475"></a>  var clock = new goog.testing.MockClock(true);
<a name="line476"></a>
<a name="line477"></a>  var mozBeforePaint = goog.testing.recordFunction();
<a name="line478"></a>  goog.events.listen(window, &#39;MozBeforePaint&#39;, mozBeforePaint);
<a name="line479"></a>
<a name="line480"></a>  window.mozRequestAnimationFrame(null);
<a name="line481"></a>  assertEquals(0, mozBeforePaint.getCallCount());
<a name="line482"></a>
<a name="line483"></a>  clock.tick(goog.testing.MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT);
<a name="line484"></a>  assertEquals(1, mozBeforePaint.getCallCount());
<a name="line485"></a>  clock.dispose();
<a name="line486"></a>}
<a name="line487"></a>
<a name="line488"></a>function testClearBeforeSet() {
<a name="line489"></a>  var clock = new goog.testing.MockClock(true);
<a name="line490"></a>  var expectedId = 1;
<a name="line491"></a>  window.clearTimeout(expectedId);
<a name="line492"></a>
<a name="line493"></a>  var fn = goog.testing.recordFunction();
<a name="line494"></a>  var actualId = window.setTimeout(fn, 0);
<a name="line495"></a>  assertEquals(
<a name="line496"></a>      &#39;In order for this test to work, we have to guess the ids in advance&#39;,
<a name="line497"></a>      expectedId, actualId);
<a name="line498"></a>  clock.tick(1);
<a name="line499"></a>  assertEquals(1, fn.getCallCount());
<a name="line500"></a>  clock.dispose();
<a name="line501"></a>}
</pre>


</body>
</html>
